<template>
	<uni-nav-bar left-icon="back" left-text="返回" right-text="充值记录" title="充值" :border="false" @clickLeft="back" @clickRight="more"></uni-nav-bar>
	<view class="content">
		<view class="xuaz">
			<view class="lable-name">充值钱包</view>
			<view class="lable-select">
				<uni-data-select v-model="form.wallet_type" :localdata="walletData" @change="qiehuan()">
					
				</uni-data-select>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">充值金额</view>
			<view class="lable-select" style="width: 80%;">
				<input type="digit" v-model="form.amount" style="font-size: 14px;" placeholder="请输入充值金额"/>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">备注</view>
			<view class="lable-select" style="width: 80%;">
				<input type="text" v-model="form.notice" style="font-size: 14px;" placeholder="请输入备注"/>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 30%;">上传打款截图</view>
			<view class="lable-select" style="width: 70%;">
				<uni-file-picker v-model="form.img_url" limit="1" file-mediatype="image" file-extname="png,jpg,jpeg" @select="uploadImg"></uni-file-picker>
			</view>
		</view>
		
		<view class="xuaz">
			<view class="lable-name" style="width: 20%;">交易密码</view>
			<view class="lable-select" style="width: 80%;">
				<input type="safe-password" v-model="form.pay_pass" style="font-size: 14px;" placeholder="交易密码"/>
			</view>
		</view>
	</view>
	
	<view class="content" style="padding: 1rem;text-align: center;">
		<button style="width: 90%;background: #dd5154;color: white;height: 2.5rem; line-height: 2.5rem;" @click="doRecharge()">确认提交</button>
		<text style="padding: 1rem;text-align: center;">打款账户，请查看<text @click="companyUserName()" style="color: #dd5154;">查看公司账号</text></text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				walletData:[
					{
						value:1,
						text:'佣金钱包(余额：0.00)'
					},
					{
						value:2,
						text:'投资钱包(余额：0.00)'
					}
				],
				form:{
					wallet_type:null,
					amount:null,
					pay_pass:null,
					img:null,
					img_url:null,
					type:2,
					notice:null
				},
				imageValue:[],
				user_token:null
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo(){
				let that = this
				if(that.userInfo == null){
					uni.getStorage({
						key: 'user_token',
						success: function (res) {
							that.user_token = res.data
							that.checkUser()
						},
						fail: function(res){
							uni.showToast({
								title:'请先登录',
								icon:'none',
								success() {
									uni.redirectTo({
										url:'/pages/login/login',
									})
								}
							})
							
						}
					});
				}
			},
			checkUser(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/getUserInfo',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						if(res.data.code == 401){
							uni.removeStorage({
								key:'user_token',
								success() {
									uni.showToast({
										icon:'none',
										title:'登录已失效,请重新登录',
										success() {
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/login/login'
												})
											})
										}
									})
								}
							})
						}
						that.userInfo = res.data.data
						that.walletData.forEach(function(value){
							if(value.value == 1){
								value.text = '佣金钱包(余额：' + that.userInfo.money + ')'
							}
							if(value.value == 2){
								value.text = '投资钱包(余额：' + that.userInfo.touzi + ')'
							}
						})
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			getRechargeLog(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/recharge/index',
					method:'POST',
					header: {token:that.user_token},
					success(res) {
						that.rechargeList = res.data.data
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			uploadImg(data){
				let that = this
				uni.uploadFile({
					url: getApp().globalData.baseUrl + '/api/index/upload',
					filePath: data.tempFilePaths[0],
					success(res) {
						that.form.img = res.data
						that.form.img_url = [{
							name:'ceshi.jpg',
							extname:'jpg',
							url:getApp().globalData.baseUrl + res.data
						}]
					}
				})
			},
			doRecharge(){
				let that = this
				if(that.form.amount <= 0){
					uni.showToast({
						title:'必须大于0',
						icon:'none'
					})
					return false;
				}
				if(that.form.amount % 1 > 0){
					uni.showToast({
						title:'必须是1的倍数',
						icon:'none'
					})
					return false;
				}
				uni.request({
					url:getApp().globalData.baseUrl + '/api/recharge/doRecharge',
					method:'POST',
					header: {token:that.user_token},
					data: that.form,
					success(res) {
						if(res.data.code == 1){
							uni.showToast({
								title:res.data.msg,
								icon:'none',
								success() {
									setTimeout(function(){
										uni.navigateTo({
											url:"/pages/RechargeList/RechargeList"
										})
									},1000)
								}
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none',
							})
						}
					},
					fail() {
						uni.showToast({
							title:'网络错误',
							icon:'none',
						})
					}
				})
			},
			back(){
				uni.navigateBack()
			},
			more(){
				uni.navigateTo({
					url:"/pages/RechargeList/RechargeList"
				})
			},
			companyUserName(){
				uni.navigateTo({
					url:"/pages/CompanyUsername/CompanyUsername"
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f4f4f4;
	}
	.xuaz{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 0 0.6rem;
		padding: 0.6rem;
		border-bottom: 1px solid #f6f6f6 !important;
	}
	.lable-name{
		width: 30%;
		font-weight: bold;
		font-size: 14px;
	}
	.lable-select{
		width: 60%;
		display: flex;
	}
	.content{
		background: #fff;
		display: flex;
		flex-direction: column;
		margin-top: .7rem;
	}
	
	.lable-select >input{
		width: 100%;
	}
</style>
